package com.edgclub.edg.utils.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.SystemClock;
import android.telephony.TelephonyManager;

import com.edgclub.edg.Constant;
import com.rockerhieu.emojicon.emoji.SerializeManager;

import java.util.Random;

public class DeviceUtil {

	private static final String RANDOM_DEVICE_ID = "random_device_id"; // IMEI存入sharedPreference中的key
	private static final String DEFAULT_RANDOM_DEVICE_ID = "0000000000000000"; // 默认随机IMEI
	private static final String SHAREDPREFERENCES_RANDOM_DEVICE_ID = "randomdeviceid"; // 保存IMEI的sharedPreference文件名

	public DeviceUtil() {
		super();
	}

	/**
	 * 保存随机生成的IMEI的方法
	 * 
	 * @param context
	 * @param deviceId
	 */
	private void saveDeviceIdToSharedpreference(Context context, long deviceId) {
		SerializeManager.saveFile(deviceId + "", Constant.DIR + "/device.json");
		/*
		 * SharedPreferences sharedPreferences = context.getSharedPreferences(
		 * SHAREDPREFERENCES_RANDOM_DEVICE_ID, Context.MODE_PRIVATE); String
		 * device = String.valueOf(deviceId);
		 * sharedPreferences.edit().putString(RANDOM_DEVICE_ID,
		 * device).commit();
		 */
	}

	/**
	 * 获取随机生成的IMEI的方法
	 * 
	 * @author huyong
	 * @return
	 */
	private String getDeviceIdFromSharedpreference(Context context) {
		String str = (String) SerializeManager.loadFile(Constant.DIR
				+ "/device.json");
		/*
		 * SharedPreferences sharedPreferences = context.getSharedPreferences(
		 * SHAREDPREFERENCES_RANDOM_DEVICE_ID, Context.MODE_PRIVATE);
		 */
		// return sharedPreferences.getString(RANDOM_DEVICE_ID,
		// DEFAULT_RANDOM_DEVICE_ID);

		if (str == null) {
			str = DEFAULT_RANDOM_DEVICE_ID;
		}

		return str;
	}

	public String getVirtualIMEI(Context context) {
		String deviceidString = getDeviceIdFromSharedpreference(context);

		// Sharedpreference中没有找到，则自动生成一个，并保存下来
		if (deviceidString != null
				&& deviceidString.equals(DEFAULT_RANDOM_DEVICE_ID)) {
			// 获取随机数，并保存至sharedpreference
			long randomDeviceid = SystemClock.elapsedRealtime();
			try {
				Random rand = new Random();
				long randomLong = rand.nextLong();
				while (randomLong == Long.MIN_VALUE) {
					randomLong = rand.nextLong();
				}
				randomDeviceid += Math.abs(randomLong);
				saveDeviceIdToSharedpreference(context, randomDeviceid);
				rand = null;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			deviceidString = String.valueOf(randomDeviceid);
		}
		return deviceidString;
	}

	public String getIMEI(Context mContext) {
		TelephonyManager telephonyManager = (TelephonyManager) mContext
				.getSystemService(Context.TELEPHONY_SERVICE);
		String IMEI = telephonyManager.getDeviceId();
		if (null == IMEI || "".equals(IMEI)) {
			IMEI = getVirtualIMEI(mContext);
			if (null == IMEI) {
				IMEI = DEFAULT_RANDOM_DEVICE_ID;
			}
		}
		return IMEI;
	}
	public int getUninstallAPKInfo(Context ctx,String archiveFilePath) {
		//archiveFilePath=Environment.getExternalStorageDirectory()+"/"+"TestB.apk"
		String versionName = null;
		int versionCode=1;
		String appName = null;
		String pakName = null;
		PackageManager pm=ctx.getPackageManager();
		PackageInfo pakinfo=pm.getPackageArchiveInfo(archiveFilePath, PackageManager.GET_ACTIVITIES);
		if (pakinfo!=null) {
			ApplicationInfo appinfo=pakinfo.applicationInfo;
			versionName=pakinfo.versionName;
			versionCode=pakinfo.versionCode;
			Drawable icon=pm.getApplicationIcon(appinfo);
			appName=(String) pm.getApplicationLabel(appinfo);
			pakName=appinfo.packageName;

		}
		return versionCode;
	}
}
